package com.woniuxy.rbac.mapper;

import com.woniuxy.rbac.entity.Role;
import com.woniuxy.rbac.utils.RbacUtils;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * 角色Mapper接口
 */
public interface RoleMapper {
    /**
     * 增加角色
     *
     * @param role
     */
    @Insert("insert into rbac_role(name,description,status) values(#{name},#{description},#{status})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    void insert(Role role);

    /**
     * 查询所有角色
     *
     * @return
     */
    @Select("select id,name,description,status from rbac_role")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "description", column = "description"),
            @Result(property = "status", column = "status"),
            @Result(property = "perms", column = "id", many = @Many(select = "com.woniuxy.rbac.mapper.PermMapper.getByRoleId", fetchType = FetchType.LAZY))
    })
    List<Role> getAll(Role role);

    @Select("select count(*) from rbac_role where name=#{name}")
    int getNumOfName(String name);

    @UpdateProvider(type = SqlProvider.class, method = "update")
    //@Update("update rbac_role set name=#{name},description=#{description},status=#{status} where id=#{id}")
    public void update(Role role);

    /**
     * 根据ID查询角色
     *
     * @param id
     * @return
     */
    @Select("select * from rbac_role where id=#{id}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "description", column = "description"),
            @Result(property = "status", column = "status"),
            @Result(property = "perms", column = "id", many = @Many(select = "com.woniuxy.rbac.mapper.PermMapper.getByRoleId", fetchType = FetchType.LAZY))
    })
    public Role getById(int id);

    /**
     * 根据角色名查询角色
     *
     * @param name
     * @return
     */
    @Select("select * from rbac_role where name=#{name}")
    public Role getByName(String name);

    public class SqlProvider {
        public String update(Role role) {
//            return new SQL() {{
//                    UPDATE("book");
//                    if (book.getNumber() != null) {
//                        SET("number=#{number}");
//                    }
//                    if (book.getBookName() != null) {
//                        SET("bookName=#{bookName}");
//                    }
//                    if (book.getAuthorName() != null) {
//                        SET("authorName=#{authorName}");
//                    }
//                    if (book.getType() != null) {
//                        SET("type=#{type}");
//                    }
//                    if (book.getPrice() != null) {
//                        SET("price=#{price}");
//                    }
//                    WHERE("id=#{id}");
//                }}.toString();


            SQL sql = new SQL();
            sql.UPDATE("rbac_role");
            if (RbacUtils.isNotEmpty(role.getName())) {
                sql.SET("name=#{name}");
            }
            if (RbacUtils.isNotEmpty(role.getDescription())) {
                sql.SET("description=#{description}");
            }
            if (RbacUtils.isNotEmpty(role.getStatus())) {
                sql.SET("status=#{status}");
            }
            sql.WHERE("id=#{id}");
            return sql.toString();
//            return new SQL().UPDATE("rbac_role").SET("name=#{name}").WHERE("id=#{id}").toString();
//            return "update rbac_role set name=#{name},description=#{description},status=#{status} where id=#{id}";        }
        }


    }


}
